/*
* @Author: zhaoxiaoyang
* @Date:   2021-08-24 15:17:58
* @Last Modified by:   zhaoxiaoyang
* @Last Modified time: 2021-08-25 12:14:00
*/
'use strict'

// import { exists, open, close } from 'fs'
const fs = require('fs'),
      { ContextExclusionPlugin } = require('webpack')


let deployFilePath = path => {

  fs.readdir(path, (err, dir) => {
    if (err) {
      console.log('read dir err:', err)
      return
    }
    console.log('dir:', dir)
    dir.map(m => {
      // console.log('refine file:', m)
      fs.readFile(path + m, 'utf-8', function (err, data) {
        if(err) {
          console.error('read file err, curFolder:', m)
          deployFilePath(path + m + '/')
          return
        }
        if(/\.html$|\.css$|.js$/g.test(m)) {
          console.log('read ok:', path + m + '\n')
          data = 
                /html$|js$/.test(m) ? 
                data.replace(/=\/static\//g, '=/pre/static/'):
                /css$/.test(m) ?
                data.replace(/url\(\/static\//g, 'url(/pre/static/'):
                data
          fs.writeFile(path + m, data, 'utf-8', function (err, res) {
            // console.log('writeFile:', path, arguments)
            if (err) {
              console.log('write data error:', err, res)
              return
            }
            console.log(`this file "${m}" has been wrote.`)
          })
        } else if(/\.map$/g.test(m)) {
          fs.rm && fs.rm(path + m, function (err, data) {
            if(err) {
              console.log('remove file failed:', err)
              return
            }
            console.log(`${m} has been removed.`, data)
          })
        }
      })
    })
  })
}


/* const storage = require('electron-storage')
const filePath = './'
storage.get(filePath, (err, data) => {
  if (err) {
    console.error(err)
  } else {
    console.log(data)
  }
});
*/

deployFilePath('./dist/')


module.exports = {
  NODE_ENV: '"pre"'
}
